/**
 * 2018年12月18日上午6:28:06
 * @author 谢进财
 */
package com.baihang.project.ds.service;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baihang.framework.kit.ModelKit;
import com.baihang.framework.kit.ShiroKit;
import com.baihang.framework.kit.database.RedisKit;
import com.baihang.framework.service.BaseService;
import com.baihang.project.ds.dao.ShouCangDao;
import com.baihang.project.ds.entity.ShouCangEntity;
import com.baihang.project.ds.model.ShouCangModel;

import redis.clients.jedis.Jedis;

@Service
public class ShouCangService extends BaseService {
	private static Logger logger = LoggerFactory.getLogger(ShouCangService.class);
	private Jedis jedis = RedisKit.getJedis();
	@Autowired
	private ShouCangDao dao;
	
	public String doShouCang(int id) {
		logger.info("开始收藏操作");
		//是否有收藏
		int count = dao.queryForInt(String.format("select count(*) from t_collection where UserName='%s' and FieldId=%d", ShiroKit.getCurrentUser().getUserName(),id));
		if(count>0) {
			return "您已经收藏过";
		}
		//收藏是否达上限
		int topCount=dao.queryForInt(String.format("select count(*) from t_collection where UserName='%s'", ShiroKit.getCurrentUser().getUserName()));
		if(topCount>=Integer.valueOf(jedis.hmget("systemConfig","CollectionCount").get(0))) {
			return "您已达收藏上限";
		}
		//查询最大排序
		int maxNum = dao.queryForInt(String.format("select Max(Sort) from t_collection where UserName='%s'", ShiroKit.getCurrentUser().getUserName()));
		//进行收藏 
		ShouCangModel model = new ShouCangModel();
		model.setbShouCangId(id);
		model.setSort(maxNum+1);
		model.setType(1);
		model.setUserName(ShiroKit.getCurrentUser().getUserName());
		
		//需要进行事务操作，以后完善
		int result=dao.save(ModelKit.modelToSaveSql(model));
		if(result!=1) {
			return "收藏失败，稍后再试";
		}else {
			//房源数据上需要加1
			dao.update(String.format("update room set E_CollectionCount=E_CollectionCount+1 where D_ID=%d", id));
		}
		return "收藏成功";
	}
	//计算剩余可以收藏房源
	public int countSyShouCang() {
		int num = dao.queryForInt(String.format("select count(*) from t_collection where UserName='%s'", ShiroKit.getCurrentUser().getUserName()));
		int syNum= Integer.valueOf(jedis.hmget("systemConfig","CollectionCount").get(0))-num;
		return syNum;
	}
	//获取当前用户收藏的房源
	public List<ShouCangEntity> getShouCang(){
		List<ShouCangEntity> list = dao.queryForFangYuanList(String.format("select r.*,t.Id,t.Sort  from t_collection as t LEFT JOIN room as r on t.FieldId=r.D_ID  where t.UserName='%s' ORDER BY t.Sort", ShiroKit.getCurrentUser().getUserName()));
		return list;
	}
	
	//向上移
	public boolean doTop(int id,int sort) {
		//上一条数据排序下划一位
		int updateShang = dao.update(String.format("update t_collection set Sort=%d where UserName='%s' and Sort=%d ", sort,ShiroKit.getCurrentUser().getUserName(),sort-1));
		//当前数据排序上升一位
		int updateDangQian = dao.update(String.format("update t_collection set Sort=%d where Id=%d ", sort-1,id));
		return true;
	}
	//向下移
	public boolean doDown(int id,int sort) {
		//下一条数据排序上升一位
		logger.info(String.format("update t_collection set Sort=%d where UserName='%s' and Sort=%d ", sort-1,ShiroKit.getCurrentUser().getUserName(),sort));
		int updateXia = dao.update(String.format("update t_collection set Sort=%d where UserName='%s' and Sort=%d ", sort,ShiroKit.getCurrentUser().getUserName(),sort+1));
		//当前数据排序下划一位
		logger.info(String.format("update t_collection set Sort=%d where Id=%d ", sort+1,id));
		int updateDangQian = dao.update(String.format("update t_collection set Sort=%d where Id=%d ", sort+1,id));
		return true;
	}
	
	//删除
	public boolean doDel(int fangyuanId,int shoucangId) {
		dao.delete(String.format("delete from t_collection where Id=%d", shoucangId));
		//房源表减1
		dao.update(String.format("update room set D_CollectionCount=D_CollectionCount-1 where D_ID=%d", fangyuanId));

		return true;
	}
	
	//重置房源状态，预定还原为未售
		public void reSetState() {
			dao.update(String.format("update room set D_state=0 where D_Operation='%s' and D_state=1", ShiroKit.getCurrentUser().getUserName()));
		}
		
	
}
